<?php

class CaptchaController extends Controller {
	
	public $width = 60;
	public $height = 25;
	
	public function index() {
		$hash = $this->_hash();
		$this->session_set("CAPTCHA_HASH", $hash);
		
		$image = imagecreatetruecolor($this->width, $this->height);
		
		$black = imagecolorallocate($image, 0, 0, 0);
		$white = imagecolorallocate($image, 255, 255, 255);
		$blue = imagecolorallocate($image, 127, 157, 185);
		
		imagefilledrectangle($image, 0, 0,$this->width, $this->height, $blue);
		imagestring($image, 10, 5, 5, $hash, $white);
		
		header("Content-Type: image/png");
		imagepng($image);
	}
	
	public function _hash($len = 5) {
		$buffer = "qwertyuiopasdfghjklzxcvbnm1234567890";
		$hash = "";
		for ($i=0; $i<$len; $i++) {
			$rand = rand(0,strlen($buffer)-1);
			$hash .= substr($buffer, $rand, 1);
		}
		return $hash;
	}
} 

?>